package com.jit.app.listener;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.core.content.FileProvider;

import java.io.File;


public class InstallClickListener implements View.OnClickListener {

    Context context;

    public InstallClickListener(Context context){
        this.context = context;
    }

    @Override
    public void onClick(View v) {
        Bundle bundle = (Bundle) v.getTag();
        String pack = bundle.getString("pack");
        String apkPath = bundle.getString("apkPath");
        File file = new File(apkPath);
        //隐式意图 开启 系统内置的安装应用的Activity
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri apkUri = FileProvider.getUriForFile(context, "com.app.appmarket.myfileprovider", file);//在AndroidManifest中的android:authorities值
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }

        context.startActivity(intent);

    }
}
